package com.example.demo1.controller;

import com.example.demo1.model.JsonTest;
import com.example.demo1.model.Student;
import com.example.demo1.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-17
 * Time：15:55
 */
@RestController//@ResponseBody + @Controller
public class Hello {
    @RequestMapping("/hello")
    public String  sayHello(HttpServletRequest req, HttpServletResponse resp) {
        //延续Servlet方式
        String name = req.getParameter("name");
        return "hello world! " + name;
    }

    @GetMapping("/get1")
    public String getPara1(Integer age) {
        return "value: " + age;
    }

    @GetMapping("/get2")
    public String getPara2(String name, Integer age) {
        return name + ": " + age;
    }

    @GetMapping("/get3")
    public String getPara3(Student student) {
        return student.toString();
    }

    @GetMapping("/login")
    public String login(String username, String password) {
        return "<h1>username: "+ username + " | " + "password: " + password + "</h1>";
    }

    /**
     * 接收js对象，相当于接收多个参数
     * @param name
     * @param password
     * @return
     */
    @PostMapping("/login2")
    public HashMap<String, Object> login2(String name, String password) {
        HashMap<String, Object> ret =  new HashMap<>();
        ret.put("name", name);
        ret.put("password", password);
        return ret;
    }

    /**
     * 接收json对象
     * @param user
     * @return
     */
    @PostMapping("/login3")
    public HashMap<String, Object> login3(@RequestBody User user) {
        HashMap<String, Object> ret = new HashMap<>();
        ret.put("username", user.getUsername());
        ret.put("password", user.getPassword());
        return ret;
    }


    /**
     * 接收复杂的json对象
     * @param jsonTest
     * @return
     */
    @PostMapping("/login4")
    public HashMap<String, Object> login4(@RequestBody JsonTest jsonTest) {
        HashMap<String, Object> ret = new HashMap<>();
        ret.put("id", jsonTest.getId());
        ret.put("users", jsonTest.getUsers());
        ret.put("score", jsonTest.getScore());
        return ret;
    }


    @GetMapping("/register")
    public String register(User user) {
        return user.toString();
    }

    @PostMapping("/reg2")
    public String reg2(String name, @RequestPart("myfile") MultipartFile file) throws IOException {
        file.transferTo(new File("/Users/chenshu/Code/java-ee/springMVC/demo1/" + name + ".jpg"));
        return "success";
    }

    @GetMapping("/getck")
    public String getookie(@CookieValue("chenshu") String ck) {
        return "get cookie successfully! -> " + "chenshu: " + ck;
    }

    /**
     * 设置session，和servlet一样～
     * @param request
     * @return
     */
    @RequestMapping("/setsess")
    public String setsess(HttpServletRequest request) {
        // 获取 HttpSession 对象，参数设置为 true 表示如果没有 session 对象就创建一个 session
        HttpSession session = request.getSession(true);
        if (session != null) {
            session.setAttribute("username", "chenshu");
        }
        return "session 存储成功";
    }

    /**
     * 获得session
     * @param username
     * @return
     */
    @RequestMapping("/getsess")
    //required如果参数是false，没找到username不会报错，反之报错
    public String getsess(@SessionAttribute(value = "username", required = false) String username) {
        return "username:" + username;
    }

    /**
     * 获取header信息
     * @param userAgent
     * @return
     */
    @RequestMapping("/gethead")
    public String getMethod(@RequestHeader("User-Agent") String userAgent) {
        return "User-Agent : " + userAgent;
    }

    /**
     * 参数重命名
     * @param time
     * @return
     */
    @RequestMapping("/gettime")
    public String getTime(@RequestParam("t") String time) {
        return "time: " + time;
    }

    /**
     * 从url中获取参数
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login4/{name}/{password}")
    public String login4(@PathVariable("name") String username,
                         @PathVariable String password) {
        return "name: " + username + " | " + "password: " + password;
    }
}
